package com.dsl.tms.repository;

import com.dsl.tms.entity.db.Message;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface MessageRepository extends JpaRepository<Message,Integer> {

    @Query(value = "SELECT title,to_char(adddate,'yyyy-mm-dd'),id FROM message WHERE towho = :towho AND readed = '1'",nativeQuery = true)
    public List<Object[]> findReadedList(@Param("towho") int towho);

    @Query(value = "SELECT title,to_char(adddate,'yyyy-mm-dd'),id FROM message WHERE towho = :towho AND readed = '0'",nativeQuery = true)
    public List<Object[]> findUnreadList(@Param("towho") int towho);

    @Query(value = "SELECT title,to_char(trashdate,'yyyy-mm-dd'),message_id FROM message_trash WHERE towho = :towho ",nativeQuery = true)
    public List<Object[]> findTrashList(@Param("towho") int towho);

    @Query(value = "SELECT m.content FROM Message  m WHERE m.id = :message_id ")
    public List<Object> findContentById(@Param("message_id") int message_id);

    @Modifying
    @Query(value = "UPDATE message m SET m.readed = 1 WHERE m.id = :message_id",nativeQuery = true)
    public void hasRead(@Param("message_id") int message_id);

    public Integer countAllByTowhoAndReaded(int towho,int readed);
}
